package com.hui.platform.falseworkapi.common.security.jwt;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hui.platform.falseworkapi.service.business.user.entity.TbUser;
import com.hui.platform.falseworkapi.service.business.user.service.TbUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户认证配置类
 * @author hui
 */
@Component
public class UserDetailsServiceImpl implements UserDetailsService {

	@Autowired
	private TbUserService tbUserService;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		
		List<GrantedAuthority> grantAuths = new ArrayList<>();
		grantAuths.add(new SimpleGrantedAuthority("ROLE_USER"));

		/**
		 * User参数：1.用户名 2.密码 3.认证信息（角色）
		 */
		TbUser user = tbUserService.getOne(new LambdaQueryWrapper<TbUser>().eq(TbUser::getUsername, username.trim()));
		return user != null ? new User(username, user.getPassword(), grantAuths) : null;
	}
}
